package com.cmbyc.todolist.utils.Token;

import com.cmbyc.todolist.exception.ToException;
import com.cmbyc.todolist.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;

public class AuthenticationInterceptor implements HandlerInterceptor {
    @Autowired
    UserInfoService userInfoService;
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object) throws Exception {
        String token = httpServletRequest.getHeader("todolist_token");// 从 http 请求头中取出 token
        // 如果不是映射到方法直接通过
        if(!(object instanceof HandlerMethod)){
            return true;
        }
        HandlerMethod handlerMethod=(HandlerMethod)object;
        Method method=handlerMethod.getMethod();
        //检查是否有passtoken注释，有则跳过认证
        if (method.isAnnotationPresent(PassToken.class)) {
            PassToken passToken = method.getAnnotation(PassToken.class);
            if (passToken.required()) {
                return true;
            }
        }
        if (token == null) {
            throw new ToException(0,"用户未登录，请登录");
        }
        // 获取 token 中的 userInfo b_Id
//        String b_Id;
//        try {
//            b_Id = JWT.decode(token).getAudience().get(0);
//        } catch (JWTDecodeException j) {
//            throw new ToException(1,"未知token");
//        }

        HttpSession session=httpServletRequest.getSession();
        if(session==null){
            throw new ToException(0,"用户未登录，请登录");
        }
        if( session.getAttribute("todolist_token")==null){
            throw new ToException(0,"用户未登录，请登录");
        }
        String sessionToken= session.getAttribute("todolist_token").toString();
        if(sessionToken.isEmpty()||!sessionToken.equals(token)){
            throw new ToException(0,"用户未登录，请登录");
        }
//        UserInfo userInfo = userInfoService.getUserByBId(b_Id);

//        if (userInfo == null) {
//            throw new ToException(2,"用户不存在，请重新登录");
//        }
        // 验证 token
//        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(userInfo.getUserPassword())).build();
//
//        try {
//            jwtVerifier.verify(token);
//        } catch (JWTVerificationException e) {
//            throw new ToException(1,"未知token");
//        }
        return true;

    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}